﻿@{
    ViewBag.Title = "推广收入分析";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="row">
    <div class="col-lg-12">
        <div class="main-box">
            <header class="main-box-header clearfix">
                <h2>推广收入分析</h2>
            </header>
            <div class="main-box-body clearfix">
                <form class="form-inline" role="form">
                    <div class="row">
                        <div class="col-lg-12" style="margin-bottom:15px">
                            <button type="button" onclick="StartDate(1)" class="btn btn-success btn-sm">本月</button>
                            <button type="button" onclick="StartDate(2)" class="btn btn-success btn-sm">上月</button>
                            <button type="button" onclick="StartDate(3)" class="btn btn-success btn-sm">昨天</button>
                            <button type="button" onclick="StartDate(4)" class="btn btn-info btn-sm">最近7天</button>
                            <button type="button" onclick="StartDate(5)" class="btn btn-danger btn-sm">最近15天</button>
                            <button type="button" onclick="StartDate(6)" class="btn btn-danger  btn-facebook btn-sm">最近30天</button>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-calendar-o"></i></span>
                            <input type="text" class="form-control" id="Starttime">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="input-group">
                            <span class="add-on input-group-addon"><i class="fa fa-calendar-o"></i></span>
                            <input type="text" class="form-control" id="Endtime">
                        </div>
                    </div>
                    <div class="form-group">    
                        <div class="radio">
                            <input type="radio" name="Income" id="optionsRadios1" value="2" checked>
                            <label for="optionsRadios1"> &nbsp; &nbsp; &nbsp; &nbsp;结算收入报表 </label>
                        </div>
                        <div class="radio">
                            <input type="radio" name="Income" id="optionsRadios2" value="1">
                            <label for="optionsRadios2">
                                &nbsp; &nbsp; &nbsp; &nbsp;预估收入报表
                            </label>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="input-group">
                            <button type="button" class="btn btn-s btn-success" id="search"> &nbsp; &nbsp; 查询&nbsp; &nbsp;</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<div class="row" id="tablelist" style="display:none">
    <div class="col-lg-12">
        <div class="main-box clearfix">
            <header class="main-box-header clearfix">
                <h2>推广结算收入分析报表</h2>
            </header>
            <div class="main-box-body clearfix">
                <div class="table-responsive">
                    <div class="DTTT btn-group">
                        <a onclick="method5('table-example')" class="btn btn-primary DTTT_button_xls" id="ToolTables_table-example_5">
                            <span>导出数据到Excel</span>
                        </a>
                    </div>
                    <table id="table-example" class="table table-hover">
                        <thead>
                            <tr>
                                <th>推广者账号</th>
                                <th>推广者分佣</th>
                                <th>下级分佣</th>
                                <th>下下级分佣</th>
                                <th>合计分佣</th>
                                <th>支付宝账号</th>
                                <th>支付宝名称</th>
                            </tr>
                        </thead>
                        
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
@section Scripts{
<link rel="stylesheet" type="text/css" href="/css/libs/dataTables.fixedHeader.css" >
<link rel="stylesheet" type="text/css" href="/css/libs/dataTables.tableTools.css" >
<script src="/js/jquery.dataTables.js" ></script>
<script src="/js/dataTables.fixedHeader.js" ></script>
<script src="/js/dataTables.tableTools.js"></script>
<script src="/js/jquery.dataTables.bootstrap.js" ></script>




    <script type="text/javascript">

        
        var idTmr;
        function getExplorer() {
            var explorer = window.navigator.userAgent;
            if (explorer.indexOf("MSIE") >= 0) {
                return 'ie';
            }
            else if (explorer.indexOf("Firefox") >= 0) {
                return 'Firefox';
            }
            else if (explorer.indexOf("Chrome") >= 0) {
                return 'Chrome';
            }
            else if (explorer.indexOf("Opera") >= 0) {
                return 'Opera';
            }
            else if (explorer.indexOf("Safari") >= 0) {
                return 'Safari';
            }
        }
        function method5(tableid) {
            if (getExplorer() == 'ie') {
                var curTbl = document.getElementById(tableid);
                var oXL = new ActiveXObject("Excel.Application");
                var oWB = oXL.Workbooks.Add();
                var xlsheet = oWB.Worksheets(1);
                var sel = document.body.createTextRange();
                sel.moveToElementText(curTbl);
                sel.select();
                sel.execCommand("Copy");
                xlsheet.Paste();
                oXL.Visible = true;

                try {
                    var fname = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls");
                } catch (e) {
                    print("Nested catch caught " + e);
                } finally {
                  
                    oWB.SaveAs(fname);
                    oWB.Close(savechanges = false);
                    oXL.Quit();
                    oXL = null;
                    idTmr = window.setInterval("Cleanup();", 1);
                }

            }
            else {
                tableToExcel(tableid)
            }
        }
        function Cleanup() {
            window.clearInterval(idTmr);
            CollectGarbage();
        }
        var tableToExcel = (function () {
            var uri = 'data:application/vnd.ms-excel;base64,',
                    template = '<html><head><meta charset="UTF-8"></head><body><table>{table}</table></body></html>',
                    base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) },
                    format = function (s, c) {
                        return s.replace(/{(\w+)}/g,
                                function (m, p) { return c[p]; })
                    }
            return function (table, name) {
                if (!table.nodeType) table = document.getElementById(table)
                var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
                window.location.href = uri + base64(format(template, ctx))
            }
        })()
        function initAcTable()
        {
            var lang = {
                "sProcessing": "处理中...",
                "sZeroRecords": "没有匹配结果",

                "sInfoPostFix": "",

                "sUrl": "",
                "sEmptyTable": "表中数据为空",
                "sLoadingRecords": "载入中..."

            };
            $('#table-example').dataTable().fnDestroy();
            var table = $("#table-example").dataTable({
                bPaginate: false,
                "bInfo": true,
                language: lang,
                autoWidth: false,
                stripeClasses: ["odd", "even"],
                processing: true,
                serverSide: false,
                searching: false,
                orderMulti: true,
                order: [],
                renderer: "bootstrap",
                columnDefs: [{
                    "targets": 'nosort',
                    "orderable": false
                }],
                ajax: function (data, callback, settings) {
                    var param = {};
                    param.Starttime = $("#Starttime").val();
                    param.Endtime = $("#Endtime").val();
                    param.Income = $("input[name='Income']:checked").val();
                    $.ajax({
                        type: "Post",
                        url: "/GetPayIncome.html",
                        cache: false,
                        data: param,
                        dataType: "json",
                        success: function (result) {
                            setTimeout(function () {

                                var returnData = {};
                                returnData.data = result.Datas.Rows;
                                callback(returnData);
                            }, 200);
                        }
                    });
                },
                columns: [
                    { "data": "UserName" },
                    { "data": "Income" },
                    { "data": "ChildIncome" },
                   { "data": "LowestIncome" },
                   { "data": "SumIncome" },
                { "data": "AlipayID" },
                   { "data": "AliName" }
                ]
            }).api();
        }
        $(document).ready(function () {
            $("#Starttime").val(Get7day(-29));
            $("#Endtime").val(formatDate(now));
            $("#search").bind("click", function () {
                var html = $("input[name='Income']:checked").val() == 2 ? "推广（结算）收入分析报表" : "推广（预估）收入分析报表"
                $(".main-box-header h2").html(html);
                initAcTable();
                $("#tablelist").show();
            });

        });
        var now = new Date();
        var nowDay = now.getDate();
        var nowMonth = now.getMonth();
        var nowYear = now.getFullYear();
        var lastMonthDate = new Date();
        lastMonthDate.setDate(1);
        lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
        var lastYear = lastMonthDate.getYear();
        var lastMonth = lastMonthDate.getMonth();

        function formatDate(date) {
            var myyear = date.getFullYear();
            var mymonth = date.getMonth() + 1;
            var myweekday = date.getDate();

            if (mymonth < 10) {
                mymonth = "0" + mymonth;
            }
            if (myweekday < 10) {
                myweekday = "0" + myweekday;
            }
            return (myyear + "-" + mymonth + "-" + myweekday);
        }

        function getMonthStartDate() {
            var monthStartDate = new Date(nowYear, nowMonth, 1);

            return nowYear + "-" + (nowMonth + 1) + "-" + "1";
        }

        function GetYesterday() {
            var monthEndDate = new Date(nowYear, nowMonth, nowDay - 1);
            return formatDate(monthEndDate);
        }
        function Get7day(day) {
            var monthEndDate = new Date(nowYear, nowMonth, nowDay + day);
            return formatDate(monthEndDate);
        }

        function getMonthEndDate() {
            var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
            return formatDate(monthEndDate);
        }

        function getLastMonthStartDate() {
            var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
            return formatDate(lastMonthStartDate);
        }

        function getMonthDays(myMonth) {
            var monthStartDate = new Date(nowYear, myMonth, 1);
            var monthEndDate = new Date(nowYear, myMonth + 1, 1);
            var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
            return days;
        }

        function getLastMonthEndDate() {
            var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));

            return formatDate(lastMonthEndDate);
        }

        function StartDate(obj) {
            switch (obj) {
                case 1:
                    $("#Starttime").val(getMonthStartDate());
                    $("#Endtime").val(formatDate(now));
                    break;
                case 2:
                    $("#Starttime").val(getLastMonthStartDate());
                    $("#Endtime").val(getLastMonthEndDate());
                    break;
                case 3:
                    $("#Starttime").val(GetYesterday());
                    $("#Endtime").val(GetYesterday());
                    break;
                case 4:
                    $("#Starttime").val(Get7day(-6));
                    $("#Endtime").val(formatDate(now));
                    break;
                case 5:
                    $("#Starttime").val(Get7day(-14));
                    $("#Endtime").val(formatDate(now));
                    break;
                case 6:
                    $("#Starttime").val(Get7day(-29));
                    $("#Endtime").val(formatDate(now));
                    break;
            }
        }

    
      

    </script>
}








